package paylayer

import (
	"strconv"
	//"strings"
	//"dhfshop/models/misc"
	"encoding/json"
	"dhfshop/models/isv"
	"dhfshop/utils/math"
	"dhfshop/models/shopping"
	"github.com/astaxie/beego"
	"thirdpaysdk/api/qcurepay"
	"dhfshop/service/redisgo"
	"github.com/pkg/errors"
)

func OrderQcurePay(appId,openId string,acPay *isv.AcPayParams,order *shopping.ValueOrder)(rsp PayResponse,prepayId string, err error) {
	orgCode := acPay.MchId
	merchCode := acPay.SubMchId
	appsecret := acPay.Appkey
	//currency := acPay.MediaId
	orderStr := strconv.FormatInt(order.OrderId,10)
	des := "订单" + "[" + orderStr + "]"
	notifyUrl := beego.AppConfig.String("WXPayNotify") + "/public/qcurepay/notify"
	amount := math.MultiTotalPriceInt(order.Payment, 100)
	qcurePayRsp, err := qcurepay.MicroAppPay(orgCode,merchCode,appsecret,orderStr,des,appId,openId,notifyUrl,int(amount))
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	//更新订单支付方式编号
	order.PayWayCode = 18
	shopping.UpdateOrderPayWayCode(order)
	rsp.WxpayRsp = qcurePayRsp.PayInfo
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderStr
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.SubMchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "ESHOP_ORDER_PAY"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func ChargeQcurePay(appId,openId,outTradeNo string,acPay *isv.AcPayParams,fee float32)(rsp PayResponse,prepayId string,err error){
	orgCode := acPay.MchId
	merchCode := acPay.SubMchId
	appsecret := acPay.Appkey
	//currency := acPay.MediaId
	des := "账户充值" + "[" + outTradeNo + "]"
	notifyUrl := beego.AppConfig.String("WXPayNotify") + "/public/qcurepay/notify"
	amount := math.MultiTotalPriceInt(fee, 100)
	qcurePayRsp, err := qcurepay.MicroAppPay(orgCode,merchCode,appsecret,outTradeNo,des,appId,openId,notifyUrl,int(amount))	 
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	rsp.WxpayRsp = qcurePayRsp.PayInfo
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + outTradeNo
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.SubMchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "MEMBER_CHARGE"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func CommonQcurePay(appId,openId,orderId,body,kind string,acPay *isv.AcPayParams,fee float32)(rsp PayResponse,prepayId string, err error){
    orgCode := acPay.MchId
	merchCode := acPay.SubMchId
	appsecret := acPay.Appkey
	//currency := acPay.MediaId
	notifyUrl := beego.AppConfig.String("WXPayNotify") + "/public/qcurepay/notify"
	amount := math.MultiTotalPriceInt(fee, 100)
	qcurePayRsp, err := qcurepay.MicroAppPay(orgCode,merchCode,appsecret,orderId,body,appId,openId,notifyUrl,int(amount))
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	rsp.WxpayRsp = qcurePayRsp.PayInfo
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderId
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.SubMchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = kind
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}
